# coding: UTF-8
# Filename : points_record_action.py
# Company : imedia
# Author : rose
# -*-coding:utf-8 -*-

from api.common.common import *
from api.common import mysql,log,utils,httputils,const,xml_tpl,err_tpl
from django.shortcuts import render_to_response
import points_record_manage

#我的积分
def my_points(request):
    #记录用户脚印(必做动作)
    httputils.footprint(request)
    #通信验证
    if validate(request):
        return validate(request)
    user_id = httputils.post(request, 'user_id')
    cus_id = httputils.post(request, 'cus_id')
    size = httputils.post(request, 'size')
    if not utils.is_num(size) or size == 0: size = 5
    p_info = points_record_manage.get_mypoints_info(user_id, cus_id, size)
    return render_to_response('points_record/success_info.xml', {'p_info':p_info}, mimetype="application/xhtml+xml")
    
    
#积分列表 前端显示
def record_list(request):
    #记录用户脚印(必做动作)
    httputils.footprint(request)
    #通信验证
    if validate(request):
        return validate(request)
    user_id = httputils.post(request, 'user_id')
    cus_id = httputils.post(request, 'cus_id')
    size = httputils.post(request, 'size')
    page_num = httputils.post(request, 'page_num')
    if not utils.is_num(size) or size == 0: size = 5
    if not utils.is_num(page_num) or page_num == 0: page_num = 1
    points_records = points_record_manage.get_points_list(user_id, cus_id, size, page_num)
    return render_to_response('points_record/success_list.xml', {'points_records':points_records}, mimetype="application/xhtml+xml")

#扫码签到接口
def scan_signin(request):
    #记录用户脚印(必做动作)
    httputils.footprint(request)
    #通信验证
    if validate_get(request):
        return validate_get(request)
    user_id = httputils.get(request, 'user_id')
    cus_id = httputils.get(request, 'cus_id')
    chain_part_id = httputils.get(request, 'chain_part_id')
    if not utils.is_num(user_id): user_id = 0
    if not utils.is_num(cus_id): cus_id = 0
    if not utils.is_num(chain_part_id): chain_part_id = 0
    (flag, si) = points_record_manage.do_scan_signin(user_id, cus_id, chain_part_id)
    if flag :
        return render_to_response('points_record/success_scan.xml', {'si':si}, mimetype="application/xhtml+xml")
    else :
        if si == -1:
            msg = "您已签到成功，谢谢！"
            return render_to_response('points_record/error_scan.xml', {'msg':msg}, mimetype="application/xhtml+xml")
        elif si == 0:
            msg = "对不起,非本店签到二维码,请重新正确扫描本店提供的签到二维码,完成签到"
            return render_to_response('points_record/error_scan.xml', {'msg':msg}, mimetype="application/xhtml+xml")
        elif si == -2:
            msg = "没有此二维码对象信息"
            return render_to_response('points_record/error_scan.xml', {'msg':msg}, mimetype="application/xhtml+xml")

#首次登陆提示会员信息
def first_login(request):
    #记录用户脚印(必做动作)
    httputils.footprint(request)
    #通信验证
    if validate(request):
        return validate(request)
    cus_id = httputils.post(request, 'cus_id')
    if not utils.is_num(cus_id): cus_id = 0
    
    contents = points_record_manage.get_login_info(cus_id)
    return render_to_response('points_record/login_info.xml', {'contents':contents}, mimetype="application/xhtml+xml")
    
#分享成功后调用积分增加接口
def after_share(request):
        #记录用户脚印(必做动作)
    httputils.footprint(request)
    #通信验证
    if validate(request):
        return validate(request)
    user_id = httputils.post(request, 'user_id')
    cus_id = httputils.post(request, 'cus_id')
    obj_type = httputils.post(request, 'obj_type')
    obj_id = httputils.post(request, 'obj_id')
    if not utils.is_num(user_id): user_id = 0
    if not utils.is_num(cus_id): cus_id = 0
    
    (rst, points_num) = points_record_manage.record_css(user_id, cus_id, 'share', obj_type, 3, obj_id)
    if rst:
        success_info = 1
        msg = '分享成功积分增加%s分数' % points_num
    else:
        success_info = 0
        msg = '积分增加失败'
    return render_to_response('points_record/share_success.xml', {'success':success_info, 'msg':msg, 'points_num':points_num}, mimetype="application/xhtml+xml")
        
        
    
    
